package com.lanou3g.dllo.baidumusic.musiclibrary.playlist;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.lanou3g.dllo.baidumusic.R;
import com.lanou3g.dllo.baidumusic.base.BaseFragment;
import com.lanou3g.dllo.baidumusic.main.MainActivity;
import com.lanou3g.dllo.baidumusic.tools.MyRvOnClickListener;
import com.lanou3g.dllo.baidumusic.tools.MyVolley;
import com.lanou3g.dllo.baidumusic.urlvalues.UrlValues;

/**
 * Created by dllo on 16/6/17.
 */
public class PlaylistFragment extends BaseFragment {

    private RecyclerView playlistRecyclerview;
    private PlaylistAdapter playlistAdapter;
    private PlaylistBean playlistBean;
    private SwipeRefreshLayout playlistSwipeRefreshLayout;
    private GridLayoutManager gridLayoutManager;
    private int lastVisibleItem;
    private int num = 2;

    @Override
    public int setLayout() {
        return R.layout.fragment_playlist;
    }

    @Override
    public void initView(View view) {

        playlistRecyclerview = (RecyclerView) view.findViewById(R.id.playlist_recyclerview);
        playlistSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.playlist_swiperefreshlayout);
        playlistAdapter = new PlaylistAdapter(context);
        playlistBean = new PlaylistBean();
        playlistAdapter.setMyRvOnClickListener(new MyRvOnClickListener() {
            @Override
            public void onClick(int position) {
                MainActivity mainActivity = (MainActivity) getActivity();
                String listid = playlistAdapter.getPlaylistBean().getContent().get(position).getListid();
                mainActivity.toPlaylistDetail(listid);
            }
        });
    }

    @Override
    public void initData() {

        refresh();
        RequestQueue requestQueue = PlaylistSingleQueue.getPlaylistSingleQueue(context).getRequestQueue();

        PlaylistGsonRequest<PlaylistBean> gsonRequest = new PlaylistGsonRequest<PlaylistBean>(Request.Method.GET, UrlValues.PLAY_LIST_URL,
                new Response.Listener<PlaylistBean>() {
                    @Override
                    public void onResponse(PlaylistBean response) {
                        playlistBean = response;
                        playlistAdapter.setPlaylistBean(response);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
        }, PlaylistBean.class

        );

        requestQueue.add(gsonRequest);
        playlistRecyclerview.setAdapter(playlistAdapter);
        gridLayoutManager = new GridLayoutManager(context,2);
        playlistRecyclerview.setLayoutManager(gridLayoutManager);
    }

    // 上拉加载方法
    private void refresh() {
        playlistSwipeRefreshLayout.setProgressViewOffset(false, 0,(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,24,getResources().getDisplayMetrics()));
        playlistRecyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == playlistAdapter.getItemCount()){
                    String url = "http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.diy.gedan&page_no=" + num + "&page_size=30&from=ios&version=5.2.3&from=ios&channel=appstore";
                    MyVolley.getMyVolley().initGson(url, new MyVolley.GetGsonListener<PlaylistBean>() {
                        @Override
                        public void getGsonListener(PlaylistBean data) {
                            playlistBean = data;
                            playlistSwipeRefreshLayout.setRefreshing(false);
                            playlistAdapter.addPlaylistBean(playlistBean);
                        }
                    },PlaylistBean.class);

                    playlistSwipeRefreshLayout.setRefreshing(true);
                    num += 1;
                }
            }
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItem = gridLayoutManager.findLastVisibleItemPosition();
            }
        });
    }


}
